//
//  THJDataView.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/1.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJDataView.h"
#import "THJDataBannerCell.h"
#import "THJDataClassCell.h"
#import "THJDataSellersCell.h"
#import "THJDataProduceCell.h"
#import "UIButton+ImageTitleSpacing.h"
#import <MJRefresh.h>
#import "UIButton+ImageTitleSpacing.h"
@interface THJDataView ()<UITableViewDelegate,UITableViewDataSource,THJDataSellersCellDelegate>
{
    UITableView * _tableview;
    NSInteger pageNo;
}
@property(nonatomic,strong)NSMutableArray * lunboArr;
@property(nonatomic,strong)NSMutableArray * classBtnArr;
@property(nonatomic,strong)NSMutableArray * productArr;
@property(nonatomic,strong)NSMutableArray * storeArr;
@end
@implementation THJDataView
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setUI];
    }
    return self;
}
-(NSMutableArray*)lunboArr{
    if(!_lunboArr){
        _lunboArr = [[NSMutableArray alloc]init];
    }
    return _lunboArr;
}
-(NSMutableArray*)productArr{
    if(!_productArr){
        _productArr = [[NSMutableArray alloc]init];
    }
    return _productArr;
}
-(void)setUI{
    pageNo = 1;
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-45-49 - 20) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJDataBannerCell" bundle:nil] forCellReuseIdentifier:@"banner"];
     [_tableview registerNib:[UINib nibWithNibName:@"THJDataClassCell" bundle:nil] forCellReuseIdentifier:@"class"];
    [_tableview registerNib:[UINib nibWithNibName:@"THJDataSellersCell" bundle:nil] forCellReuseIdentifier:@"seller"];
     [_tableview registerNib:[UINib nibWithNibName:@"THJDataProduceCell" bundle:nil] forCellReuseIdentifier:@"product"];
    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableview];
    [self getData];
    [self getProductListDataWithPage:@"1" CatId:nil];
}
-(void)loadMore{
    pageNo ++;
    [self getProductListDataWithPage:[NSString stringWithFormat:@"%ld",pageNo] CatId:nil];
    NSLog(@"加载更多");
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 180;
    }else if(indexPath.row == 1){
        return 180;
    }else if(indexPath.row == 2){
        return 365;
    }else{
        return 110;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.productArr.count+3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        THJDataBannerCell * cell = [tableView dequeueReusableCellWithIdentifier:@"banner" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBannerWithImgUrlArr:self.lunboArr Height:180];
        return cell;
    }else if(indexPath.row == 1){
        THJDataClassCell * cell = [tableView dequeueReusableCellWithIdentifier:@"class" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSLog(@"cell.subview's count-%ld",cell.contentView.subviews.count);
        for (NSInteger i = 0; i<10; i++) {
            UIButton * btn = (UIButton*) cell.contentView.subviews[i];
            NSDictionary * dic = self.classBtnArr[i];
            [btn setTitle:dic[@"name"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnsClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn sd_setButtonImageWithUrl:dic[@"logo"] placeHolderImgWithName:@""];
            btn.tag = i + 100;
            [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
        }
        return cell;
    }else if(indexPath.row == 2){
        THJDataSellersCell * cell = [tableView dequeueReusableCellWithIdentifier:@"seller" forIndexPath:indexPath];
        [cell setContentWithArr:self.storeArr];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        THJDataProduceCell * cell = [tableView dequeueReusableCellWithIdentifier:@"product" forIndexPath:indexPath];
        if(self.productArr.count>0){
            NSDictionary * dic = self.productArr[indexPath.row - 3];
            [cell setCellContentWithDic:dic];
        }
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row >= 3){
        NSDictionary * dic = self.productArr[indexPath.row - 3];
        [self.delegate BtnclickWithIndex:indexPath.row clickType:@"cellClick" Dic:dic];
    }else{
        [self.delegate BtnclickWithIndex:indexPath.row clickType:@"seller" Dic:nil];
    }
    
}

/**
 获取数据
 */
-(void)getData{
    __weak typeof(self) weakself = self;
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=index&r=index"];
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"10-btn-%@",receiveData);
        if([receiveData[@"status"]isEqual:@"200"]){
            
            NSArray * array = receiveData[@"data"][@"lunbo_data"];
            weakself.classBtnArr  = receiveData[@"data"][@"cat"];
            weakself.storeArr  = receiveData[@"data"][@"company"];
            for (NSDictionary * dic in array) {
                [weakself.lunboArr addObject:dic[@"image_src"]];
            }
            [_tableview reloadData];
        }
    }];
}
//最新发布列表
-(void)getProductListDataWithPage:(NSString*)page CatId:(NSString*)catid{
    __weak typeof(self) weakself = self;
    NSString * urlStr = [NSString stringWithFormat:@"%@%@%@&page=%@",THJUrlDomain,THJindexPHP,@"f=sell_5&r=sell_5",page];
    NSLog(@"供应列表url-%@",urlStr);
    [THJNetWork GetDataWithGetRequestTypeUrl:urlStr Callback:^(NSDictionary* receiveData, NSError *error) {
        [_tableview.mj_footer endRefreshing];
        NSLog(@"receiveData-%@",receiveData);
        for (NSDictionary * dic in receiveData[@"data"]){
            [weakself.productArr  addObject:dic];
        }
        [_tableview reloadData];
    }];
}
#pragma mark --系别点击--
-(void)btnsClick:(UIButton*)btn{
    NSDictionary * dic = self.classBtnArr[btn.tag - 100];
    [self.delegate BtnclickWithIndex:btn.tag-100 clickType:@"btnsClick" Dic:dic];
}
#pragma mark --公司点击--
-(void)companClickIndex:(NSInteger)index{
    NSLog(@"%@",self.storeArr[index-1000]);
    NSDictionary * dic = self.storeArr[index-1000];
    [self.delegate companyClickWithInfoDic:dic];
}
#pragma mark ---更多公司---
-(void)getMoreCompany{
    [self.delegate getMoreCompany];
}
@end
